#include "IGraph.h"

std::unique_ptr <IGraph> MakeCompactGraph(const std::vector < std::vector <int> > &graph)
{
    size_t number_of_edges = 0;
    for (size_t node = 0; node < graph.size(); node++)
        number_of_edges += graph.at(node).size();
    std::unique_ptr <IGraph> matrix_graph(new MatrixGraph());
    std::unique_ptr <IGraph> list_graph(new ListGraph());
    if (matrix_graph -> byte_size(graph.size(), number_of_edges) < list_graph -> byte_size(graph.size(), number_of_edges))
    {
        matrix_graph.release();
        matrix_graph.reset(new MatrixGraph(graph.size()));
        for (size_t node = 0; node < graph.size(); node++)
            for (auto it = graph.at(node).begin(); it != graph.at(node).end(); it++)
                matrix_graph -> add_edge(node, *it);
        return std::move(matrix_graph);
    }
    else
    {
        list_graph.release();
        list_graph.reset(new ListGraph(graph));
        return std::move(list_graph);
    }
}
